package com.jic.product.controller.manage;

import com.github.pagehelper.PageInfo;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.product.common.util.BeanUtil;
import com.jic.product.constant.consist.ResponseCode;
import com.jic.product.entity.Category;
import com.jic.product.entity.vo.request.CategoryPage;
import com.jic.product.exception.AppRuntimeException;
import com.jic.product.manage.request.CategoryRequest;
import com.jic.product.manage.request.CommonIdRequest;
import com.jic.product.manage.request.FirstCategoryEditRequest;
import com.jic.product.manage.response.CategoryPageResponse;
import com.jic.product.manage.response.CategoryResponse;
import com.jic.product.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by ZW on 2020/1/9 11:04
 * 商品类目管理
 */
@RestController
@RequestMapping("/category")
@Api(value = "商品设置", description = "类目管理", tags = "商品设置")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 分页查询
     */
    @ApiOperation(value = "获取所有商品类目信息")
    @ResponseBody
    @PostMapping(value = "/queryPage")
    public RestResult<PageResult<CategoryPageResponse>> queryPage(@RequestBody CategoryRequest form) {
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        Category category = BeanUtil.copy(form, Category.class);
        PageResult<Category> result = categoryService.queryPage(category, page);
        List<CategoryPageResponse> categoryPageResponseList = result.getRows().stream().map(temp -> {
            CategoryPageResponse categoryPageResponse = BeanUtil.copy(temp, CategoryPageResponse.class);
            return categoryPageResponse;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<CategoryPageResponse> pageResult = new PageResult<>();
        pageResult.setRows(categoryPageResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);

    }

    /**
     * 一级类目新增
     */
    @ResponseBody
    @PostMapping(value = "/insertFirstCategory")
    public RestResult insertFirstCategory(@RequestBody FirstCategoryEditRequest firstCategoryEditRequest) {
        RestResult restResult = new RestResult();
        log.info("开始进入商品属性新增==========》" + firstCategoryEditRequest.toString());
        try {
            restResult = categoryService.insert(firstCategoryEditRequest);
        } catch (Exception e) {
            log.error("一级类目新增------>>{}", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION,e.getMessage());
        }
        return restResult;
    }


    /**
     * 商品类目修改
     */
    @ResponseBody
    @PostMapping(value = "/updateCategory")
    public RestResult updateCategory(@RequestBody FirstCategoryEditRequest firstCategoryEditRequest) {
        return categoryService.categoryUpdate(firstCategoryEditRequest);
    }

    /**
     * 商品类目加价修改
     */
    @ResponseBody
    @PostMapping(value = "/updateIncrement")
    public RestResult updateIncrement(@RequestBody FirstCategoryEditRequest firstCategoryEditRequest) {
        return categoryService.updateIncrement(firstCategoryEditRequest);
    }

    /**
     * 商品类目新增
     */
    @ResponseBody
    @PostMapping(value = "/addCategorySon")
    public RestResult addCategory(@RequestBody FirstCategoryEditRequest firstCategoryEditRequest) {
        return categoryService.categoryAdd(firstCategoryEditRequest);
    }

    /**
     * 商品品牌删除
     */
    @ResponseBody
    @PostMapping(value = "/delete")
    public RestResult delete(@RequestBody FirstCategoryEditRequest firstCategoryEditRequest) {
        return categoryService.delete(firstCategoryEditRequest.getId());
    }

    /**
     * 后台商品类目查询
     * type=1  后台类目
     * 数据组装
     */
    @ResponseBody
    @PostMapping(value = "/queryBehindCategory")
    public RestResult queryBehindCategory(@RequestBody CategoryRequest categoryRequest) {
        categoryRequest.setType(1);
        categoryRequest.setParentId(String.valueOf(0));
        try {
            List<CategoryResponse> categoryResponses = categoryService.queryBehindCategory(categoryRequest);
            return RestResult.success(categoryResponses);
        } catch (Exception e) {
            log.error("前台商品类目查询======>{}", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }

    }

    /**
     * 前台商品类目查询
     * type=2  前台类目
     * mybatis
     */

    @PostMapping(value = "/queryBeforeCategory")
    @ResponseBody
    public RestResult queryBeforeCategory(@RequestBody CategoryRequest categoryRequest) {
        categoryRequest.setType(2);
        RestResult restResult = new RestResult();
        try {
            List<CategoryResponse> categoryResponses = categoryService.queryBeforeCategory(categoryRequest);
            restResult.setData(categoryResponses);
            restResult.setCode("0000");
            restResult.setMsg("调用成功");
        } catch (Exception e) {
            log.error("前台商品类目查询======>{}", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }

        return restResult;

    }

    /**
     * 根据类目id查询
     */
    @ResponseBody
    @PostMapping(value = "/queryCategoryById")
    public RestResult queryCategoryById(@RequestBody CategoryRequest categoryRequest) {
        String id = categoryRequest.getId();
        RestResult restResult = new RestResult();
        try {
            Category category = categoryService.queryByPrimaryKey(Long.valueOf(id));
            restResult.setData(category);
            restResult.setCode("0000");
            restResult.setMsg("调用成功");
        } catch (Exception e) {
            e.printStackTrace();
            restResult = RestResult.error("9999", e.getLocalizedMessage());
        }

        return restResult;
    }


    /**
     * 查询前台一级类目
     */
    @ResponseBody
    @PostMapping(value = "/queryRestCategory")
    public RestResult queryRestCategory() {
        log.info("开始调用查询前台一级类目，{}");
        try {
            List<Category> categoryList = categoryService.queryRestCategory();
            return RestResult.success(categoryList);
        } catch (Exception e) {
            log.error("开始调用查询前台一级类目======>{}", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }

    }


    /*
     *查询当前二级类目的所有子类目（包含二级类目）
     * type=1  后台类目
     * level=2  二级类目
     * */
    @ResponseBody
    @PostMapping(value = "/queryAfterSecondSonCategory")
    public RestResult queryAfterSecondSonCategory(@RequestBody CommonIdRequest commonIdRequest) {
        log.info("jic-product===============》查询当前二级类目的所有子类目,{}", commonIdRequest.getId());
        CategoryRequest categoryRequest = new CategoryRequest();
        categoryRequest.setType(1);
        categoryRequest.setId(String.valueOf(commonIdRequest.getId()));
        try {
            List<CategoryResponse> categoryResponses = categoryService.queryAfterSecondSonCategory(categoryRequest);
            return RestResult.success(categoryResponses);
        } catch (Exception e) {
            log.error("查询当前二级类目的所有子类目--->", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }

    }


    /*
     *判断当前组织是否新增后台类目
     * */
    @ResponseBody
    @PostMapping(value = "/isAddAfterCategory")
    public RestResult isAddAfterCategory(@RequestBody CategoryRequest categoryRequest) {
        log.info("jic-product===============》判断当前组织是否新增后台类目,{}", categoryRequest.toString());
        Category category=new Category();
        category.setDeptId(categoryRequest.getAdminOperate().getDeptId());
        category.setType(1);
        try {
            List<Category> categoryList = categoryService.query(category);
            if(CollectionUtils.isNotEmpty(categoryList)){
                return RestResult.success(Boolean.FALSE);
            }else{
                return RestResult.success(Boolean.TRUE);
            }
        } catch (Exception e) {
            log.error("判断当前组织是否新增后台类目--->", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }

    }



    /**
     * 后台商品类目查询
     * type=1  后台类目
     * 数据组装
     */
    @ResponseBody
    @PostMapping(value = "/queryMerchantBehindCategory")
    public RestResult queryMerchantBehindCategory(@RequestBody CategoryRequest categoryRequest) {
        return  RestResult.success(categoryService.queryMerchantBehindCategory(categoryRequest));
    }

    @ResponseBody
    @PostMapping(value = "/selectCategory")
    public RestResult selectCategory(@RequestBody Category request) {
        List<CategoryResponse> responseList = categoryService.selectCategory(request);

        return  RestResult.success(responseList);
    }
    @ResponseBody
    @PostMapping(value = "/selectPageCategory")
    @ApiOperation(value = "分页查询加价配置管理")
    public RestResult<PageResult<List<CategoryResponse>>> selectPageCategory(@RequestBody CategoryPage request) {
        return  categoryService.selectPageCategory(request);
    }
}
